package com.ansion.log;

import android.text.TextUtils;

import org.json.JSONObject;

/**
 * @author john on 2020/11/6.
 * @version 1.0
 * @desc
 */
public class LogFilter {
    private static final String CKEY = "ckey";
    private static final String BODY = "\"body\"";
    private static final String IKEY = "\"iKey\"";
    private static final String HMACKEY = "\"hmacKey\"";
    private static final String KEY = "key";
    private static final String PASSWORD = "password";
    private static final String PPASSWORD = "Password";
    private static final String SECURITYTOKEN = "\"securityToken\"";
    private static final String KEYSECRET = "\"accessKeySecret\"";
    private static final String KEYID = "\"accessKeyId\"";
    private static final String INFO_C = "info";
    private static final String FID = "fid";
    private static final String MD5_C = "md5";
    private static final String PUB = "pub";
    private static final String TRIGGER = "trigger";
    private static final String URL_C = "url";
    private static final String PUSHREG = "PushReg";
    private static final String HASH = "hash";
    private static final String PADDING = "padding";
    private static final String PHONE = "phone";
    private static final String NAME = "\"name\"";

    public static String criticalMsg(String originMsg) {
        if (TextUtils.isEmpty(originMsg)) {
            return "";
        }
        if (originMsg.contains(CKEY)) {
            return CKEY;
        }
//        if (originMsg.contains(BODY)) {
//            return filterMsg(originMsg, BODY);
//        }
        if (originMsg.contains(IKEY)) {
            return filterMsg(originMsg, IKEY);
        }
        if (originMsg.contains(HMACKEY)) {
            return filterMsg(originMsg, HMACKEY);
        }
        if (originMsg.contains(KEY)) {
            return filterMsg(originMsg, KEY);
        }
        if (originMsg.contains(PASSWORD)) {
            return filterMsg(originMsg, PASSWORD);
        }
        if (originMsg.contains(PPASSWORD)) {
            return filterMsg(originMsg, PPASSWORD);
        }
        if (originMsg.contains(SECURITYTOKEN)) {
            return filterMsg(originMsg, SECURITYTOKEN);
        }
        if (originMsg.contains(KEYSECRET)) {
            return filterMsg(originMsg, KEYSECRET);
        }
        if (originMsg.contains(KEYID)) {
            return filterMsg(originMsg, KEYID);
        }
        if (originMsg.contains(PHONE)) {
            return filterMsg(originMsg, PHONE);
        }
        if (originMsg.contains(NAME)) {
            return filterMsg(originMsg, NAME);
        }
//        if (originMsg.contains(INFO_C)) {
//            return INFO_C;
//        }
//        if (originMsg.contains(FID)) {
//            return FID;
//        }
//        if (originMsg.contains(MD5_C)) {
//            return MD5_C;
//        }
//        if (originMsg.contains(PUB)) {
//            return PUB;
//        }
//        if (originMsg.contains(TRIGGER)) {
//            return TRIGGER;
//        }
//        if (originMsg.contains(URL_C)) {
//            return URL_C;
//        }
//        if (originMsg.contains(PUSHREG) && (originMsg.contains(HASH) || originMsg.contains(PADDING))) {
//            return PUSHREG;
//        }
        return originMsg;
    }

    private static String filterMsg(String originMsg, String kWord) {
        int index = originMsg.indexOf(kWord);
        if (index > 0) {
            return originMsg.substring(0, index) + kWord;
        }
        return kWord;
    }

    public static String filterMsgDe(String originMsg) {
        StringBuilder sb = new StringBuilder();
        try {
            JSONObject jsonObject = new JSONObject(originMsg);
            String c = jsonObject.optString("c");
            String f = jsonObject.optString("f");
            String t = jsonObject.optString("t");
            if (!TextUtils.isEmpty(c)) {
                sb.append(c).append("/").append(f).append("/").append(t).append("/");
                JSONObject jsonM;
                switch (c) {
                    case "Event":
                    case "GroupsAction":
                        jsonM = jsonObject.optJSONObject("m");
                        if (jsonM != null) {
                            sb.append(jsonM.optString("name", "Non-name"));
                        }
                        break;
                    case "HyperText":
                        jsonM = jsonObject.optJSONObject("m");
                        if (jsonM != null) {
                            sb.append(jsonM.optString("uuid", "Non-uuid")).append("/")
                                    .append(jsonM.optString("MIMETYPE", "Non-MIMETYPE"));
                        }
                        break;
                    case "Call":
                        jsonM = jsonObject.optJSONObject("m");
                        if (jsonM != null) {
                            sb.append(jsonM.optString("id", "Non-id")).append("/")
                                    .append(jsonM.optString("state", "Non-state"));
                        }
                        break;
                    default:
                        break;
                }
            }
        } catch (Throwable e) {
            // e.printStackTrace();
        }
        return sb.toString();
    }
}
